#!/bin/bash

#    / **  
#    *〈一句话功能简述〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [参数1说明]  
#    * @param [参数2] [参数2说明]  
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function fun1()
{  
    msgstring="helld world !";
    echo "[Debug]: ${msgstring}";
    echo "[INFO]:  ${msgstring}";
    echo "[WARN]:  ${msgstring}";
    echo "[ERROR]: ${msgstring}";
	# 在shell中：
	# $0: 获取当前脚本的名称
	# $#: 传递给脚本的参数个数
	# $$: shell脚本的进程号
	# $1, $2, $3...：脚本程序的参数

    # $?: 获取上一命令或函数打印输出结果

    # 拆分字符串 
    # 原理：
    # ${parameter//pattern/string} 
    # 用string来替换parameter变量中所有匹配的pattern
    # 例子：以 , 拆分字符串
    #  string="hello,shell,split,test" 
    #  array=(${string//,/ }) 
    #  echo "[Debug] 循环输出 array[]所有值="${array[1]}
    # for var in ${array[@]}
    # do
    #    echo $var
    # done
    #  参考 [Shell_Linux Shell 中实现字符串切割的几种方法](https://blog.csdn.net/u010003835/article/details/80750003)

    # shell中各种括号的作用()、(())、[]、[[]]、{}
    # 参考资料 [hell中各种括号的作用](https://blog.csdn.net/taiyang1987912/article/details/39551385)
}

#    / **  
#    *〈 判断shell系统类型 〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [参数1说明]  
#    * @param [参数2] [参数2说明]  
#    * @return [返回类型说明]  String [linux、osx、bsd] 
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function getShellOStype()
{
    SHELL_PLATFORM='unknown';
    ## echo "[Debug] SHELL_PLATFORM=${SHELL_PLATFORM}"

    ## echo $OSTYPE | tr '[A-Z]' '[a-z]'"
    shellostypestring=`echo $OSTYPE`;
    ## echo "[Debug] shellostypestring=${shellostypestring}"

    ## 从字符串中识别shell系统类型
    case "${shellostypestring}" in
        *'linux'*   ) SHELL_PLATFORM='linux'    ;;
        *'darwin'*  ) SHELL_PLATFORM='osx'      ;;
        *'bsd'*     ) SHELL_PLATFORM='bsd'      ;;
    esac

    # if [[ $SHELL_PLATFORM == 'linux' || $SHELL_PLATFORM == 'bsd' ]]; then
    #    echo "[Debug] linux"
    # elif [[ $SHELL_PLATFORM == 'osx' ]]; then
    #    # $SHELL_PLATFORM == 'bsd'
    #    echo "[Debug] osx Shell"
    # elif [[ $SHELL_PLATFORM == 'bsd' || $SHELL_PLATFORM == 'osx'  ]]; then
    #    echo "[Debug] bad shell"
    # else
    #    echo "[Debug] unknown"
    # fi

    # 返回值
    echo $SHELL_PLATFORM;
}


#    / **  
#    *〈获得linux发行版本名称〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [参数1说明]  
#    * @param [参数2] [参数2说明]  
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function getDistName()
{
  if grep -Eqii "Arch Linux" /etc/issue || grep -Eq "Arch Linux" /etc/*-release; then
        DISTRO='Arch Linux'
        PM='pacman'
    elif grep -Eqii "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
        DISTRO='CentOS'
        PM='yum'
    elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then
        DISTRO='RHEL'
        PM='yum'
    elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then
        DISTRO='Aliyun'
        PM='yum'
    elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
        DISTRO='Fedora'
        PM='yum'
    elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
        DISTRO='Debian'
        PM='apt'
    elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
        DISTRO='Ubuntu'
        PM='apt'
    elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then
        DISTRO='Raspbian'
        PM='apt'
    elif grep -Eqi "Alibaba Cloud Linux" /etc/issue || grep -Eq "Alibaba Cloud Linux" /etc/*-release; then
        DISTRO='Alibaba Cloud Linux'
        PM='yum'
    else
        DISTRO='unknow'
    fi

    # 返回值
    echo $DISTRO;
}


function Add—Path-ENV() {
    # 定义要添加的软件目录
    # SOFTWARE_DIR="/path/to/your/software/bin"
    TARGET_PATH="$1"
     if [ -z "$TARGET_PATH" ]; then
        xecho "ERROR" "错误：请指定要添加的软件目录路径作为参数"
        # echo "用法: $0 /path/to/your/software"
        exit 1
    fi
    
    # 安全扩展波浪号路径（关键修复）
    if [[ "$TARGET_PATH" == ~* ]]; then
        expanded_path=$(eval echo "$TARGET_PATH")
        if [ "$expanded_path" != "$TARGET_PATH" ]; then
            TARGET_PATH="$expanded_path"
            xecho "DEBUG" "扩展波浪号路径: $TARGET_PATH"
        fi
    fi

    # 检查路径是否存在
    if [ ! -e "$TARGET_PATH" ]; then
        xecho "ERROR" "错误：指定路径不存在: $TARGET_PATH"
        exit 1
    fi
    
    # 如果是文件，则取其所在目录
    if [ -f "$TARGET_PATH" ]; then
        SOFTWARE_DIR=$(dirname "$TARGET_PATH")
        xecho “INFO” "检测到文件路径，将使用其所在目录: $SOFTWARE_DIR"
    elif [ -d "$TARGET_PATH" ]; then
        SOFTWARE_DIR="$TARGET_PATH"
    else
        xecho “ERROR” "错误：不支持的路径类型: $TARGET_PATH"
        exit 1
    fi

    # 确保路径是绝对路径
    case $SOFTWARE_DIR in
        /*) ;;
        *) 
            xecho "WARNING" "警告：路径不是绝对路径，将转换为绝对路径"
            SOFTWARE_DIR=$(realpath "$SOFTWARE_DIR" 2>/dev/null || echo "$(pwd)/$SOFTWARE_DIR")
            ;;
    esac

    # 获取当前Shell类型（取基名）
    current_shell=$(basename "$SHELL")
    xecho "INFO" "获取当前Shell类型: $current_shell"
    # 确定配置文件路径
    case "$current_shell" in
        bash)
            config_file="$HOME/.bashrc"
            ;;
        zsh)
            config_file="$HOME/.zshrc"
            ;;
        sh|dash|ksh)
            # 注意：sh可能是其他Shell的链接，使用通用配置文件
            config_file="$HOME/.profile"
            ;;
        *)
            xecho "ERROR" "Unsupported shell: $current_shell"
            exit 1
            ;;
    esac

    # 检查目录是否已在PATH中
    path_contains_dir() {
        echo "$PATH" | tr ':' '\n' | grep -qFx "$1"
    }

    # 主逻辑
    if path_contains_dir "$SOFTWARE_DIR"; then
        xecho "INFO" "目录已存在于PATH中: $SOFTWARE_DIR"
    else
        # 备份配置文件
        cp "$config_file" "${config_file}.bak-$(date +%Y%m%d%H%M%S)"
        
        # 添加环境变量配置
        echo "添加目录到PATH: $SOFTWARE_DIR"
        echo "export PATH=\"\$PATH:$SOFTWARE_DIR\"" >> "$config_file"
        
        # 提示用户
        echo '''
        配置已更新！请执行以下命令生效：
        立即生效: source "$config_file"
        或重启终端
        备份文件: ${config_file}.bak-*
        '''
    fi
}